package cn.flying.cloud.oauth.server.configuration.captcha;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;

import cn.flying.cloud.base.common.enums.RtCodeEnum;
import cn.flying.cloud.oauth.server.configuration.exception.CaptchaValidException;


/**
 * @description: 提供一个校验，判断验证码校验是否通过
 * @author: admin
 * @date: 2023年05月26日 13:12
 * @version: 1.0
 */
public class CaptchaAuthenticationProvider extends DaoAuthenticationProvider {

    @Override
    protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
        // detail里面原来存的是ip地址与sessionId,现在加多了一个属性代表验证码是否正确
        if (!((CaptchaWebAuthenticationDetails) authentication.getDetails()).isPassed()) {
            throw new CaptchaValidException(RtCodeEnum.R10004.getDesc());
        }
        //校验密码
        super.additionalAuthenticationChecks(userDetails, authentication);
    }
}